import json


async def save_chat_to_json(new_chat, file_name):
    try:
        with open(file_name, 'r', encoding='utf-8') as f:
            chats = json.load(f)

        # 添加新的对话到列表中
        chats.append(new_chat)

        # 将数据保存到 JSON 文件中
        with open(file_name, 'w', encoding='utf-8') as f:
            json.dump(chats, f, ensure_ascii=False)
    except Exception as e:
        print(f"将聊天保存到文件时出错: {e}")
